/*
*/

#ifndef __PCASE_H
#define __PCASE_H

typedef struct _BEAM		BEAM;
typedef struct _FORM		FORM;
typedef struct _KEY		KEY;
typedef struct _RPTBUFFER	RPTBUFFER;
typedef struct _RPTDATA		RPTDATA;
typedef struct _RINFO		RINFO;
typedef struct _RPTWAVE		RPTWAVE;
typedef struct _SERIAL		SERIAL;
typedef struct _THUNK		THUNK;

#ifdef	WIN32

#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include "resource.h"

typedef short	S16;
typedef int	S32;
typedef	BYTE	U8;
typedef	WORD	U16;
typedef	DWORD	U32;

extern BEAM		form;
extern HCURSOR		defcursor;
extern HINSTANCE	instance;		//current instance
extern HPEN		grypen;
extern HPEN		redpen;
extern RINFO		rinfo;
extern TCHAR		csfram[];
extern TCHAR		cserch[];

#else	//WIN32

#include "stm32f10x.h"

#ifdef DEBUG
#define	ASSERT(x,y)	Assert(x,y)
#else
#define	ASSERT(x,y)
#endif

#define Transmitter  0x00
#define Receiver     0x01

#define	WAV_NIL		0xffff

typedef	s16	S16;
typedef	s32	S32;
typedef	u8	U8;
typedef	u16	U16;
typedef	u32	U32;

extern	U8	addr;
extern	U8	i2c_addr7;
extern	u32	clock;
extern	u32	usb;
extern	RINFO	info;
extern	BEAM	beam;
//extern	BEAM	signal;
extern	KEY	keywave[];

void	Assert	(u8*,u32);
void	Dummy	(void);
void	Fault	(void);
void	Page	(u32);

#endif	//WIN32

#define	BEAM_LEN	496

#define	CNT_MASK	0x3fffff
#define	CNT_SHIFT	22
#define	countof(a)		(sizeof(a)/sizeof(*(a)))

#define	HID_VENDOR	0x0482
#define	HID_PRODUCT	0x5750
#define	VER_FIRMWARE	0x0001

#ifndef max
#define max(a,b)	(((a)>(b))?(a):(b))
#endif

#define	MOD_NIL		0
#define	MOD_SEARCH	1
#define	MOD_SIGNAL	2
#define	MOD_TEST	3
#define	MOD_PRESET	4
#define	MOD_LEARNING	5

#define	PAG_TV		0
#define	PAG_CBL		1
#define	PAG_DVD		2
#define	PAG_AUX		3
#define	PAG_MAX		4

#define	RPT_INFO	1
#define	RPT_MSG		2
#define	RPT_WAVE	3
#define	RPT_GDATA	4
#define	RPT_TDATA	5
#define	RPT_EDATA	6
#define	RPT_HDATA	7
#define	RPT_MAP		8

#define	SLOT_SHIFT	8

struct _THUNK{
    U32	data[15];
};

struct _BEAM{
    U8	key;
    U8	repetition;
    U8	exit;
    U8	bits;
    U8	head0;
    U8	head1;
    U8	max0;
    U8	max1;
    U16	period;
    U16	pulse;
    U16	head;
    U16	tail;
    U32	time;
    U32	timeout;
    U32	base;
    union{
	U32	form[496];
	THUNK	thunk[33];
	U16	wave[496];
    };
};

struct _KEY{
    U8	vendor;
    U8	extra;
    U16	data1;
    U32	data2;
};

struct _SERIAL{
    U32	num[3];
};

struct _RPTBUFFER{
    U8	report;
    U8	type;
    U8	data[63];
};

struct _RPTDATA{
    U8	type;
    U8	data[63];
};

struct _RINFO{
    U8		type;
    U8		mode;
    U8		page;
    U8		key;
    SERIAL	serial;
    U32		tick;
    S16		acc[3];
    U16		version;
    U16		vcc;
    U16		vbat;
    U16		vsen;
    U16		tempa;
    U16		tempb;
    U16		channel;
    U16		devices[4];
};

struct _RPTWAVE{
    U8		type;
    U8		seq;
    U8		last;
    U8		reserved;
    THUNK	thunk;
};

#endif